home *** CD-ROM | disk | FTP | other *** search
/ ...taking it to the Macs! / ...taking it to the Macs!.iso / Extras / ActiveX Mac SDK / ActiveX SDK / Common / debug.cpp < prev    next >
Text File  |  1997-01-03  |  3KB  |  149 lines

  1. //
  2. //  DEBUG.CPP
  3. //
  4. //  Copyright (C) Microsoft Corporation, 1996
  5. //
  6.  
  7. #define CHECKMEMORYLEAKS
  8. #define FAR
  9.  
  10. #include <ole2.h>
  11. #include <dispatch.h>
  12. #include <wintypes.h>
  13. #include "debug.h"
  14. #include <stdio.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <Types.h>
  18.  
  19. #pragma export on
  20.  
  21. #ifdef CHECKMEMORYLEAKS
  22.  
  23. #undef CoTaskMemAlloc
  24. #undef CoTaskMemRealloc
  25. #undef CoTaskMemFree
  26. #define PTRTABLEENTRIES     512
  27. static BOOL fFirst = TRUE;
  28. static LPVOID PtrTable[PTRTABLEENTRIES];
  29. ULONG g_cAllocations = 0;
  30.  
  31. void *
  32. DebugCoTaskMemAlloc(unsigned long cb)
  33. {
  34.     LPVOID ptr;
  35.  
  36.     if (fFirst) {
  37.         memset(PtrTable, 0, sizeof(PtrTable));
  38.         fFirst = FALSE;
  39.     }
  40.  
  41.     ptr = CoTaskMemAlloc(cb);
  42.  
  43.     if (ptr != NULL) {
  44.            int i;
  45.  
  46.         for (i = 0; i < PTRTABLEENTRIES; i++) {
  47.             if (PtrTable[i] == NULL) {
  48.                 PtrTable[i] = ptr;
  49.                 g_cAllocations++;
  50.                 break;
  51.             }
  52.         }
  53.         if (i == PTRTABLEENTRIES) {
  54.             DebugPrintf("eek, too many allocations for debug"); Debugger();
  55.         }
  56.     }
  57.  
  58.     return ptr;
  59. }
  60.  
  61. void *
  62. DebugCoTaskMemRealloc(void * pv, unsigned long cb)
  63. {
  64.     LPVOID ptr;
  65.  
  66.     if (pv == NULL) {
  67.         DebugPrintf("CoTaskMemRealloc: pv==NULL"); Debugger();
  68.     } else {
  69.            int i;
  70.  
  71.         for (i = 0; i < PTRTABLEENTRIES; i++) {
  72.             if (PtrTable[i] == pv) {
  73.                 PtrTable[i] = NULL;
  74.                 g_cAllocations--;
  75.                 break;
  76.             }
  77.         }
  78.         if (i == PTRTABLEENTRIES) {
  79.             DebugPrintf("CoTaskMemRealloc: %x not found, pv"); Debugger();
  80.         }
  81.     }
  82.  
  83.     ptr = CoTaskMemRealloc(pv, cb);
  84.  
  85.     if (ptr != NULL) {
  86.         int i;
  87.         
  88.         for (i = 0; i < PTRTABLEENTRIES; i++) {
  89.             if (PtrTable[i] == NULL) {
  90.                 PtrTable[i] = ptr;
  91.                 g_cAllocations++;
  92.                 break;
  93.             }
  94.         }
  95.         if (i == PTRTABLEENTRIES) {
  96.             DebugPrintf("eek, too many allocations for debug"); Debugger();
  97.         }
  98.     }
  99.  
  100.     return ptr;
  101. }
  102.  
  103. void
  104. DebugCoTaskMemFree(void * pv)
  105. {
  106.     if (pv == NULL) {
  107.         DebugPrintf("CoTaskMemFree: pv==NULL"); Debugger();
  108.     } else {
  109.         int i;
  110.         
  111.         for (i = 0; i < PTRTABLEENTRIES; i++) {
  112.             if (PtrTable[i] == pv) {
  113.                 PtrTable[i] = NULL;
  114.                 g_cAllocations--;
  115.                 break;
  116.             }
  117.         }
  118.         if (i == PTRTABLEENTRIES) {
  119.             DebugPrintf("CoTaskMemFree: %x not found", pv); Debugger();
  120.         }
  121.     }
  122.  
  123.     CoTaskMemFree(pv);
  124. }
  125.  
  126. #endif
  127.  
  128. void
  129. DebugPrintf(
  130.     char * lpFormatString,
  131.     ...
  132.     )
  133. {
  134. #if 0    //jjo
  135.     va_list arglist;
  136.     char Buffer[256];
  137.  
  138.     va_start(arglist, lpFormatString);
  139.     vsprintf(Buffer, lpFormatString, arglist);
  140.     strcat(Buffer, ";g");
  141.     va_end(arglist);
  142.     debugstr(Buffer);
  143. #else
  144.     #pragma unused (lpFormatString)
  145. #endif
  146. }
  147.  
  148. #pragma export off
  149.